<template>
    <div class="manage-session">
        <div style="height: 35px">
            <el-form :model="searchForm" :inline="true" size="mini">
                <el-form-item label="name">
                    <el-input v-model="searchForm.name"></el-input>
                </el-form-item>
                <el-form-item label="code">
                    <el-input v-model="searchForm.code"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="listSession">Search</el-button>
                    <el-button type="primary" @click="listSession">Refresh</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table
                border
                default-expand-all
                highlight-current-row
                :data="sessionTable"
                row-key="sessionId"
                height="500"
                size="mini"
                style="width: 100%"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column
                    prop="host"
                    label="host"
                    width="*">
            </el-table-column>
            <el-table-column
                    prop="startTime"
                    label="startTime"
                    width="230">
            </el-table-column>
            <el-table-column
                    prop="lastAccessTime"
                    label="lastAccessTime"
                    width="230">
            </el-table-column>
            <el-table-column
                    prop="user.account"
                    label="account"
                    width="*">
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="145">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row.sessionId)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
  import {listSession, logoutSession} from "../../../api/manage/session";

  export default {
    name: "Session",
    data () {
      return {
        sessionTable: [],
        searchForm: {},
      }
    },
    methods: {
      handleDelete (ukId) {
        this.$confirm('此操作将注销该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          logoutSession(ukId).then(result => {
            this.listSession()
            this.$message({
              type: 'success',
              message: '注销成功!'
            });
          }).catch(error => {
            console.log(error)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消注销'
          });
        });
      },
      listSession () {
        listSession(this.searchForm, 1, 30).then(result => {
          this.sessionTable = result.data.sessions
        }).catch(error => {
          console.log(error)
        })
      }
    },
    created () {
      this.listSession()
    }
  }
</script>

<style scoped>
    .manage-session {
        width: 100%;
        height: 100%;
    }

    /* element ui样式重写 */
    .manage-session > .el-table {
        height: calc(100% - 35px) !important;
    }

    .manage-session > .el-table > .el-table__body-wrapper {
        height: calc(100% - 35px) !important;
    }
</style>